//
//  LYStuWebViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/15.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYStuWebViewController.h"
#import "LYClassWebViewController.h"
#import "WebViewJavascriptBridge.h"

#define stuMainUrl @"http://esj.zzzedu.cn/webview/view/student/index.php?"

@interface LYStuWebViewController ()<UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *webView;

@property (copy, nonatomic) NSString *urlStr;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomBarHeight;

@property (nonatomic, copy) NSString *ID;
@property WebViewJavascriptBridge *bridge;
@end

@implementation LYStuWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.fd_prefersNavigationBarHidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setUpWebView];
    [self buildBridge];
    [self checkPersonInfo];
    
}


#pragma mark - 构造方法
+ (instancetype)stuWebVCWithID:(NSString *) ID{
    
    LYStuWebViewController *webVC = [[self alloc] init];
    webVC.ID = ID;
    return webVC;
}

#pragma mark - 重写方法
- (void)setID:(NSString *)ID{

    _ID = [ID copy];
    
    NSString *parameter = [NSString stringWithFormat:@"to_user_id=%@&token=%@",self.ID,LYToken];
    self.urlStr = [stuMainUrl stringByAppendingString:parameter];
    


}

#pragma mark - 初始化
/**
 *  初始化webView
 */
- (void)setUpWebView{
    
    //自适应屏幕
    self.webView.scalesPageToFit = YES;
    
    //加载请求
    if(self.urlStr){
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
        
        [self.webView loadRequest:request];
    }
}

/**
 *  桥接JS
 */
- (void)buildBridge{
    
    if (_bridge) { return; }
    
    __weak typeof(self) weakSelf = self;
    
    [WebViewJavascriptBridge enableLogging];
    
    _bridge = [WebViewJavascriptBridge bridgeForWebView:_webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
        
        NSLog(@"receivedMessage");
        
    }];
    
    //返回桥接
    [_bridge registerHandler:@"goback" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        [weakSelf.navigationController popViewControllerAnimated:YES];
        
    }];
    
    
    //课程桥接
    [_bridge registerHandler:@"openCourseDetail" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        LYClassWebViewController *classVC = [LYClassWebViewController classWebVCWithID:data[@"course_id"]];
        [weakSelf.navigationController pushViewController:classVC animated:YES];
        
    }];
}



#pragma mark - 网络请求
/**
 *  查看好友信息
 */
- (void)checkPersonInfo{

    [LYRequestTool getOtherInfoWithToken:LYToken to_user_id:self.ID success:^(LYPersonalInfo *info) {
       
        self.bottomBarHeight.constant = info.is_friend ? 0 : 60;
        
    } failure:^(NSString *code, NSString *message) {
        
        
        
    }];


}


/**
 *  申请好友
 */
- (void)applyFriend{
    
    [MBProgressHUD showMessage:nil];
    [LYRequestTool addFriendssWithToken:LYToken userID:self.ID success:^{
        
        [MBProgressHUD hideHUD];
        self.bottomBarHeight.constant = 0;
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
    }];
}

#pragma mark - 按钮点击

- (IBAction)applyBtnOnclick:(UIButton *)sender {
      [[VVYCheckStatus sharedInstance] checkIsLoginBlock:^(LYUserInfoModel *userInfo) {
    [self applyFriend];
      }];
}

#pragma mark - webView代理

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    [MBProgressHUD showMessage:nil];
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [MBProgressHUD hideHUD];
    
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    
    [MBProgressHUD hideHUD];
    [MBProgressHUD showError:@"网络不给力" toView:webView];
}


@end
